******************* GRAPH 1 --- 4I POSTMATERIALISM BY COHORT *******************

*Load the raw dataset from World Value Survey
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Set up the svy environment with population weights
svyset S007 [pweight=S018]

** Keep only varaibles to be used: 
keep Y002 A193 S018 X002 s002 S003

** Gen the region variable 
gen region = ""
* Re-assign based on regions
replace region = "Western Europe" if inlist(S003, 20, 40, 208, 246, 250, 276, 300, 352, 372, 380, 442, 528, 578, 620, 724, 752, 756, 826, 909)
replace region = "Eastern Europe" if inlist(S003, 8, 70, 100, 112, 191, 196, 203, 233, 348, 428, 440, 498, 499, 616, 642, 688, 703, 705, 804, 807, 915)
replace region = "United States" if S003 == 840
replace region = "Africa" if inlist(S003, 12, 231, 288, 404, 434, 466, 504, 566, 646, 682, 710, 716, 788, 800, 818, 854, 894)
replace region = "China" if inlist(S003, 156, 344, 446)
replace region = "Russia" if S003 == 643
replace region = "Japan" if S003 == 392
replace region = "South Korea" if S003 == 410
drop if region == ""

**Generate the Cohort variable
gen cohort = .
replace cohort = 1930 if X002 >= 1930 & X002 < 1940
replace cohort = 1940 if X002 >= 1940 & X002 < 1950
replace cohort = 1950 if X002 >= 1950 & X002 < 1960
replace cohort = 1960 if X002 >= 1960 & X002 < 1970
replace cohort = 1970 if X002 >= 1970 & X002 < 1980
replace cohort = 1980 if X002 >= 1980 & X002 < 1990

** Observation w/o assigned cohort
drop if cohort == . 

** Generate a string variable over the numerical 4i PostMaterialism (Y002) 
gen pm_level = ""
replace pm_level = "HIGH" if Y002 == 3 
replace pm_level = "MEDIUM" if Y002 == 2  
replace pm_level = "LOW" if Y002 == 1 
** Drop observations with Y002 < 0 
drop if pm_level == "" 

** Calculate the frequency and collapse by cohort and region
contract region cohort pm_level
** Wide dataset
reshape wide _freq, i(region cohort) j(pm_level) string

** Compute the percentages and difference
gen total = _freqHIGH + _freqLOW + _freqMEDIUM
gen pctHIGH = (_freqHIGH/total)*100
gen pctLOW  = (_freqLOW/total)*100
gen diff = pctHIGH - pctLOW

**Graph 
twoway (line diff cohort if region=="Africa",          lcolor(black)) ///
       (line diff cohort if region=="China",           lcolor(red))   ///
       (line diff cohort if region=="Eastern Europe",  lcolor(green)) ///
       (line diff cohort if region=="Japan",           lcolor(blue))  ///
       (line diff cohort if region=="Russia",          lcolor(purple)) ///
       (line diff cohort if region=="South Korea",     lcolor(yellow)) ///
       (line diff cohort if region=="United States",   lcolor(orange)) ///
       (line diff cohort if region=="Western Europe",  lcolor(teal)) ///
, legend(order(1 "Africa" 2 "China" 3 "Eastern Europe" 4 "Japan" 5 "Russia" 6 "South Korea" 7 "United States" 8 "Western Europe")) ///
  xlabel(1930(10)1980) ///
  ylabel(-50(10)20) ///
  title("4iPostMaterialism by Birth Cohort in Different Regions") ///
  xtitle("Cohort (Median Year of Birth)") ///
  ytitle("Percentage High minus Percentage Low on Postmaterialism Index", size(small))

* Export the graph as a PDF>
graph export "${graphs_path}/4iPMCohort.pdf", as(pdf) replace name("Graph")


******************* GRAPH 2 --- CARE INDEX BY COHORT *******************
use "${raw}/Trends_VS_1981_2022_stata_v4_0", clear

** Set up the svy environment with population weights
svyset S007 [pweight=S018]

** Keep only varaibles to be used: 
keep A193 S018 X002 s002 S003

** Gen the region variable 
gen region = ""
* Re-assign based on regions
replace region = "Western Europe" if inlist(S003, 20, 40, 208, 246, 250, 276, 300, 352, 372, 380, 442, 528, 578, 620, 724, 752, 756, 826, 909)
replace region = "Eastern Europe" if inlist(S003, 8, 70, 100, 112, 191, 196, 203, 233, 348, 428, 440, 498, 499, 616, 642, 688, 703, 705, 804, 807, 915)
replace region = "United States" if S003 == 840
replace region = "Africa" if inlist(S003, 12, 231, 288, 404, 434, 466, 504, 566, 646, 682, 710, 716, 788, 800, 818, 854, 894)
replace region = "China" if inlist(S003, 156, 344, 446)
replace region = "Russia" if S003 == 643
replace region = "Japan" if S003 == 392
replace region = "South Korea" if S003 == 410
drop if region == ""

**Generate the Cohort variable
gen cohort = .
replace cohort = 1930 if X002 >= 1930 & X002 < 1940
replace cohort = 1940 if X002 >= 1940 & X002 < 1950
replace cohort = 1950 if X002 >= 1950 & X002 < 1960
replace cohort = 1960 if X002 >= 1960 & X002 < 1970
replace cohort = 1970 if X002 >= 1970 & X002 < 1980
replace cohort = 1980 if X002 >= 1980 & X002 < 1990


**Observation w/o assigned cohort
drop if cohort == . 

**Levels of sanctity by observation
gen ca_level = ""
replace ca_level = "HIGH" if inlist(A193, 5, 6) 
replace ca_level = "MEDIUM" if inlist(A193, 3, 4) 
replace ca_level = "LOW" if inlist(A193, 1, 2) 

** Drop observations with A193 < 0 
drop if ca_level == ""

** Calculate the frequency and collapse by cohort and region
contract region cohort ca_level
** Wide dataset
reshape wide _freq, i(region cohort) j(ca_level) string

** Compute the percentages and difference
gen total = _freqHIGH + _freqLOW + _freqMEDIUM
gen pctHIGH = (_freqHIGH/total)*100
gen pctLOW  = (_freqLOW/total)*100
gen diff = pctHIGH - pctLOW

**Graph 
twoway (line diff cohort if region=="Africa",          lcolor(black)) ///
       (line diff cohort if region=="China",           lcolor(red))   ///
       (line diff cohort if region=="Eastern Europe",  lcolor(green)) ///
       (line diff cohort if region=="Japan",           lcolor(blue))  ///
       (line diff cohort if region=="Russia",          lcolor(purple)) ///
       (line diff cohort if region=="South Korea",     lcolor(yellow)) ///
       (line diff cohort if region=="United States",   lcolor(orange)) ///
       (line diff cohort if region=="Western Europe",  lcolor(teal)) ///
, legend(order(1 "Africa" 2 "China" 3 "Eastern Europe" 4 "Japan" 5 "Russia" 6 "South Korea" 7 "United States" 8 "Western Europe")) ///
  xlabel(1930(10)1980) ///
  ylabel(0(10)100) ///
  title("Care Index by Birth Cohort in Different Regions") ///
  xtitle("Cohort (Median Year of Birth)") ///
  ytitle("Percentage High minus Percentage Low on Care Index", size(small))

* Export the graph as a PDF>
graph export "${graphs_path}/CareCohort.pdf", as(pdf) replace name("Graph")
